package com.android.gufengmall.home;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.CommentAdapter;
import com.android.gufengmall.bean.ApparelBean;
import com.android.gufengmall.bean.CultureBean;
import com.android.gufengmall.bean.FolkCraftBean;
import com.android.gufengmall.bean.TalentBean;
import com.android.gufengmall.gen.ApparelBeanDao;
import com.android.gufengmall.gen.CommentBean;
import com.android.gufengmall.gen.CultureBeanDao;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.FolkCraftBeanDao;
import com.android.gufengmall.gen.TalentBeanDao;
import com.android.gufengmall.utils.BitMapConversionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论详细信息页面
 */
public class CommentActivity extends BaseActivity {

    private static final String TAG = "CommentActivity";

    private EditText mEdtComment;
    private Button mBtnRelease;
    private RecyclerView mRvComment;

    private CommentAdapter mCommentAdapter;

    private App mMyApp;
    private DaoSession mDaoSession;
    private ApparelBeanDao mApparelBeanDao;
    private FolkCraftBeanDao mFolkCraftBeanDao;
    private TalentBeanDao mTalentBeanDao;
    private CultureBeanDao mCultureBeanDao;

    private int mStatus;
    private List<CommentBean> mCommentBeans = new ArrayList<>();
    private List<CommentBean> mApparelBeans = new ArrayList<>();
    private List<CommentBean> mFolkCraftBeans = new ArrayList<>();
    private List<CommentBean> mTalentBeans = new ArrayList<>();
    private CultureBean mCultureBean;
    private ApparelBean mApparelBean;
    private FolkCraftBean mFolkCraftBean;
    private TalentBean mTalentBean;
    private String mPosition;
    private TextView mTvTitle;
    private ImageView mImgPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mBtnRelease.setOnClickListener(v -> {
            String edtComment = mEdtComment.getText().toString().trim();
            CommentBean commentBean = new CommentBean();
            commentBean.setComment(edtComment);
            switch (mStatus) {
                case 0:
                    mCommentBeans.add(commentBean);
                    mCultureBean.setMComments(mCommentBeans);
                    mCultureBeanDao.update(mCultureBean);
                    break;
                case 1:
                    mApparelBeans.add(commentBean);
                    mApparelBean.setComments(mApparelBeans);
                    mApparelBeanDao.update(mApparelBean);
                    break;
                case 2:
                    mFolkCraftBeans.add(commentBean);
                    mFolkCraftBean.setComments(mFolkCraftBeans);
                    mFolkCraftBeanDao.update(mFolkCraftBean);
                    break;
                case 3:
                    mTalentBeans.add(commentBean);
                    mTalentBean.setComments(mTalentBeans);
                    mTalentBeanDao.update(mTalentBean);
                    break;
            }
            mCommentAdapter.notifyDataSetChanged();
            showToast("评论成功");
        });
    }

    private void initData() {
        mStatus = getIntent().getExtras().getInt("status");
        mPosition = getIntent().getExtras().getString("position");

        mMyApp = (App) this.getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mApparelBeanDao = mDaoSession.getApparelBeanDao();
        mFolkCraftBeanDao = mDaoSession.getFolkCraftBeanDao();
        mTalentBeanDao = mDaoSession.getTalentBeanDao();
        mCultureBeanDao = mDaoSession.getCultureBeanDao();

        List<CultureBean> cultureBeanList = mCultureBeanDao.queryBuilder().where(CultureBeanDao.Properties.Title.eq(mPosition)).list();
        for (int i = 0; i < cultureBeanList.size(); i++) {
            CultureBean cultureBean = cultureBeanList.get(i);
            if (cultureBean.getTitle().equals(mPosition)) {
                mCultureBean = cultureBean;
            }
        }

        List<ApparelBean> apparelBeanList = mApparelBeanDao.queryBuilder().where(ApparelBeanDao.Properties.Title.eq(mPosition)).list();
        for (int i = 0; i < apparelBeanList.size(); i++) {
            ApparelBean apparelBean = apparelBeanList.get(i);
            if (apparelBean.getTitle().equals(mPosition)) {
                mApparelBean = apparelBean;
            }
        }

        List<FolkCraftBean> folkCraftBeanList = mFolkCraftBeanDao.queryBuilder().where(FolkCraftBeanDao.Properties.Title.eq(mPosition)).list();
        for (int i = 0; i < folkCraftBeanList.size(); i++) {
            FolkCraftBean folkCraftBean = folkCraftBeanList.get(i);
            if (folkCraftBean.getTitle().equals(mPosition)) {
                mFolkCraftBean = folkCraftBean;
            }
        }

        List<TalentBean> talentBeanList = mTalentBeanDao.queryBuilder().where(TalentBeanDao.Properties.Title.eq(mPosition)).list();
        for (int i = 0; i < talentBeanList.size(); i++) {
            TalentBean talentBean = talentBeanList.get(i);
            if (talentBean.getTitle().equals(mPosition)) {
                mTalentBean = talentBean;
            }
        }

        switch (mStatus) {
            case 0:
                if (mCultureBean != null) {
                    if (mCultureBean.getMComments() != null) {
                        mCommentBeans.addAll(mCultureBean.getMComments());
                    }
                    mCommentAdapter = new CommentAdapter(this, mCommentBeans);
                    mTvTitle.setText(mCultureBean.getTitle());
                    mImgPath.setImageBitmap(BitMapConversionUtils.stringToBitmap(mCultureBean.getImgPath()));
                }
                break;
            case 1:
                if (mApparelBean != null) {
                    if (mApparelBean.getMComments() != null) {
                        mApparelBeans.addAll(mApparelBean.getMComments());
                    }
                    mCommentAdapter = new CommentAdapter(this, mApparelBeans);
                    mTvTitle.setText(mApparelBean.getTitle());
                    mImgPath.setImageBitmap(BitMapConversionUtils.stringToBitmap(mApparelBean.getImgPath()));
                }
                break;
            case 2:
                if (mFolkCraftBean != null) {
                    if (mFolkCraftBean.getMComments() != null) {
                        mFolkCraftBeans.addAll(mFolkCraftBean.getMComments());
                    }
                    mCommentAdapter = new CommentAdapter(this, mFolkCraftBeans);
                    mTvTitle.setText(mFolkCraftBean.getTitle());
                    mImgPath.setImageBitmap(BitMapConversionUtils.stringToBitmap(mFolkCraftBean.getImgPath()));
                }
                break;
            case 3:
                if (mTalentBean != null) {
                    if (mTalentBean.getMComments() != null) {
                        mTalentBeans.addAll(mTalentBean.getMComments());
                    }
                    mCommentAdapter = new CommentAdapter(this, mTalentBeans);
                    mTvTitle.setText(mTalentBean.getTitle());
                    mImgPath.setImageBitmap(BitMapConversionUtils.stringToBitmap(mFolkCraftBean.getImgPath()));
                }
                break;
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        mRvComment.setLayoutManager(layoutManager);

        mRvComment.setAdapter(mCommentAdapter);
    }

    private void initView() {
        mTvTitle = findViewById(R.id.tv_title);
        mImgPath = findViewById(R.id.img_path);
        mEdtComment = findViewById(R.id.edt_comment);
        mBtnRelease = findViewById(R.id.btn_release);
        mRvComment = findViewById(R.id.rv_comment);
    }
}